package Asgmt1;

import Asgmt1.element.FileUtil;
import Asgmt1.element.Player;
import Asgmt1.element.Roll;
import Asgmt1.element.Score;

import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class Demo {
    public static void main(String[] args) {
        boolean flag = true;
        Scanner sc = new Scanner(System.in);
        while (flag) {
            System.out.println("Hello!\n" + "Do you want to play parctice (p) or player vs CPU (c)?");
            String choice = sc.nextLine();
            switch (choice) {
                case "p":
                    Roll roll = new Roll();
                    for (int i = 0;i < 3 ; i++){
                        System.out.println("\nPress Enter to roll.");
                        sc.nextLine();
                        roll.rollDice();
                    }
                    break;
                case "c":
                    int playerScore = 0,cpuScore = 0;
                    Roll playerRoll = new Roll();
                    Roll cpuRoll = new Roll();
                    System.out.println("What is your name?");
                    String playerName = sc.nextLine();
                    System.out.println(FileUtil.getAllScores());
                    Player player;
                    if (FileUtil.queryFileNames().contains(playerName)){
                        player = FileUtil.getGameData(playerName);
                        player.getScores().forEach(System.out::println);
                    }else {
                        player = new Player(playerName);
                    }
                    Score score = new Score();
                    for (int i = 0;i < 3 ; i++){
                        System.out.println("\nPress Enter to roll.");
                        sc.nextLine();
                        System.out.println(playerName+":");
                        playerScore += playerRoll.rollDice();
                        System.out.println("CPU:");
                        cpuScore += cpuRoll.rollDice();
                    }
                    if (playerScore > cpuScore){
                        System.out.println("---You Win!---");
                        score.setResult("WIN");
                    }else if (playerScore < cpuScore){
                        System.out.println("---You Lose!---");
                        score.setResult("LOSE");
                    }else{
                        System.out.println("---A Tie!---");
                        score.setResult("A TIE");
                    }
                    score.setScores(playerScore);
                    score.setScoreTime(new Date());
                    player.getScores().add(score);
                    FileUtil.saveGameData(player);
                    break;
            }
            System.out.println("Do you want to play again(enter) or quit(q)");
            String choice2 = sc.nextLine();
            if (choice2.equals("q")) {
                flag = false;
            }
        }
    }
}
